package com.changgou.seckill.listener;

import com.alibaba.fastjson.JSON;
import com.changgou.seckill.service.SeckillOrderService;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.annotation.RabbitListeners;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName SeckillOrderMessageListener
 * @Description 秒杀商品监听
 * @Author 洛尘
 * @Date 18:35 2019/11/29
 * @Version 2.1
 **/
@Component
@RabbitListener(queues = {"${mq.pay.queue.seckillorder}"})
public class SeckillOrderMessageListener {

    @Autowired
    private SeckillOrderService seckillOrderService;

    @RabbitHandler
    public void readSeckillOrderMessage(String text){
        Map<String,String> map = JSON.parseObject(text, Map.class);
        //Map<String , String> map = new HashMap<>();
        String return_code = map.get("return_code");
        if("SUCCESS".equals(return_code)){
            String result_code = map.get("result_code");
            String username = map.get("username");
            if("SUCCESS".equals(result_code)){
                String out_trade_no = map.get("out_trade_no");
                String transaction_id = map.get("transaction_id");
                String timeEnd = map.get("timeEnd");
                // 付款成功
                seckillOrderService.updateOrder(out_trade_no , transaction_id ,username , timeEnd);
            }else{
                // 付款失败
                seckillOrderService.deleteOrder(username);
            }


        }
    }

}
